<?php
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\MaterialAdmin;
use think\exception\ValidateException;

class Material extends BaseController
{
  /**
   * 素材列表接口
   */
  public function matList() 
  {
    $param = $this->request->get([
      'query',
      'status',
      'type',
      'offset' => $this->request->get('pagenum/d')?:1,
      'length' => $this->request->get('pagesize/d')?:20,
      'ordername' => $this->request->get('ordername')?:'mat_id',
      'order' => $this->request->get('order')?:'descending',
      'sort'
    ]);

    try {
      $adminList = MaterialAdmin::matList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($adminList);
  }

  public function matDialogList() {
    $param = $this->request->get([
      'name',
      'status',
      'type',
      'offset' => $this->request->get('page/d')? $this->request->get('page/d') + 1 :1,
      'length' => $this->request->get('size/d')?:20,
      'sort'
    ]);

    try {
      $adminList = MaterialAdmin::matDialogList($param);
    } catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode()); 
    }
    // var_dump($adminList);
    return successJson($adminList);
  }

  /**
   * 素材编辑接口
   */
  public function matEdit()
  {
    $param = $this->request->post([
      'mat_id',  // 主键id
      'mat_name', // 名称
      'mat_type', // 类型
      'mat_format', // 素材清晰度
      'markin', // 入点
      'markout', // 出点
      'creator', // 创建人
      'create_time', // 创建时间
      'check_flag', // 审核状态
      'check_man', // 审核人
      'check_time', // 审核时间
      'check_mind', // 审核意见
      'remark', // 备注
      'date_limit', // 日期限制
      's_date', // 开始时间
      'e_date', // 结束时间
      'can_play_num', // 可播次数
      'use_channel', // 可用频道
    ]);

    try {
      $this->validate($param, 'app\common\validate\Material.sceneeditsave');
      $data = MaterialAdmin::matAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 素材新增接口
   */
  public function matadd()
  {    
    $param = $this->request->post();

    if(isset($param['mat_id'])) {
      unset($param['mat_id']);
    }

    try {
      $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = MaterialAdmin::matAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /**
   * 素材删除接口
   */
  public function matdel()
  {
    $param = $this->request->post([
      'mat_id'
    ]);

    try {
      $data = MaterialAdmin::matDelete($param);
    } catch (\Exception $e)
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
      
    return successJson($data);
  }

  /* 素材类型列表 */
  public function mattypeList() {
    $param = $this->request->get([
      'id'
    ]);

    try {
      $data = MaterialAdmin::mattypeList($param);
    } catch (\Exception $e) 
    {
      return exceptionJson($e->getMessage(), $e->getCode());
    }

    return successJson($data);
  }

  /* 素材类型新增 */
  public function mattypeAdd() {
    $param = $this->request->post([
      'mat_type_name',
      'parent_type',
      'oper_rights',
      'remark'
    ]);

    if(isset($param['mat_type_id'])) {
      unset($param['mat_type_id']);
    }

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = MaterialAdmin::mattypeAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /* 素材类型编辑 */
  public function mattypeEdit() {
    $param = $this->request->post([
      'mat_type_id',
      'mat_type_name',
      'parent_type',
      'oper_rights',
      'remark'
    ]);

    try {
      // $this->validate($param, 'app\common\validate\Material.scenecopysave');
      $data = MaterialAdmin::mattypeAddEditSave($param);
    } catch (ValidateException $e) {
      return validateJson([], $e->getError());
    }  catch(\Exception $e) {
      return exceptionJson($e->getMessage(), $e->getCode());
    }
    return successJson($data);
  }

  /* 素材类型删除 */
  public function mattypeDel() {
    // 接收删除的id数组
    $ids = $this->request->delete();
    try {
      $res = MaterialAdmin::mattypeDel($ids);
    } catch(\Exception $e) {
        return exceptionJson($e->getMessage(), $e->getCode()); 
    }

    return successJson($res);
    }
}